﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*'
 * The city Buscelona (as the name suggests) has a great bus transport system. All buses have circular lines. The bus drivers in Buscelona like to chat. Fortunately most bus lines have some stops in common. If a bus driver meets a colleague on a bus stop they chat a bit and exchange all news they know.

The operation of buses is highly synchronized. The time necessary to get from one stop to the next stop is always exactly 1 minute.

Each morning each bus driver has some important news that only he knows. When a busdriver meets a colleague he will tell him all news he knows. If two bus drivers share the same start station, they will exchange their news there already (before they start working). Note that exchanging news and stopping does not take any time.

Input

The first line of a test case contains the number of bus lines n (0 < n < 50). The following n lines start with a number s (0 < s < 50) indicating the stops of a busline. On the same line follow s numbers representing a bus station each. A bus starts at the first station. When a bus reaches the last station, the bus will drive to the first station again.

There are many test cases separated by an empty line. Input data terminates with n = 0.

Output

For each test case you should output the time in minutes which it takes until all bus drivers know all news. If that never happens, your program should write the word "NEVER" (without quotes).

Example

Sample input:
3
3 1 2 3
3 2 3 1
4 2 3 4 5

2
2 1 2
2 5 8

0

Sample output:
12
NEVER
 * 
 * 
 * http://www.spoj.pl/problems/BUS/
 */
namespace CSharpAlgorithm
{
    class Bus
    {
        public Bus()
        {
            int busCount = 0;
            do
            {
                busCount = Int32.Parse(TestConsole.ReadLine());
                if (busCount == 0)
                {
                    break;
                }
                List<int>[] busStop = new List<int>[busCount];
                for (int i = 0; i < busCount; i++)
                {
                    string[] input = TestConsole.ReadLine().Split(' ');
                    int busStopCount = Int32.Parse(input[0]);
                    busStop[i] = new List<int>();
                    for (int j = 1; j <= busStopCount; j++)
                    {
                        busStop[i].Add(Int32.Parse(input[j]));
                    }
                }

                //LCM 표 만들기
                int[,] lcmBoard = new int[busCount, busCount];
                for (int x = 0; x < busCount; x++)
                {
                    for (int y = x + 1; y < busCount; y++)
                    {
                        lcmBoard[x, y] = Primes.LCM(busStop[x].Count, busStop[y].Count);
                    }
                }
                //시간 표 만들기
                Queue<int>[,] timeBoard = new Queue<int>[busCount, busCount];
                for (int x = 0; x < busCount; x++)
                {
                    for (int y = x + 1; y < busCount; y++)
                    {
                        timeBoard[x, y] = GetMeetTime(busStop[x], busStop[y], lcmBoard[x,y]);
                    }
                }
                int restCount = ((busCount * busCount) - busCount);
                bool[,] meetBoard = new bool[busCount, busCount];
                for (int i = 0; i < busCount; i++)
                {
                    meetBoard[i, i] = true;
                }
                //모든 코스가 이어져 있는지 확인
                bool[] checkBoard = new bool[busCount];
                Check(timeBoard, 0, checkBoard);
                bool connected = true;
                for (int i = 0; i < checkBoard.Length; i++)
                {
                    if (!checkBoard[i])
                    {
                        connected = false;
                        break;
                    }
                }
                if (!connected)
                {
                    Console.WriteLine("NEVER");
                }
                while (connected)
                {
                    int minX = -1, minY = -1;
                    int min = Int32.MaxValue;
                    for (int x = 0; x < busCount; x++)
                    {
                        for (int y = x + 1; y < busCount; y++)
                        {
                            if (timeBoard[x,y].Count > 0 
                                && -1 < timeBoard[x, y].Peek() 
                                && timeBoard[x, y].Peek() < min)
                            {
                                minX = x;
                                minY = y;
                                min = timeBoard[x, y].Peek();
                            }
                        }
                    }
                    int v = AddMeet(meetBoard, minX, minY, busCount);
                    restCount -= v;
                    if (restCount == 0)
                    {
                        Console.WriteLine(min);
                        break;
                    }
                    timeBoard[minX, minY].Dequeue();
                    timeBoard[minX, minY].Enqueue(min + lcmBoard[minX, minY]);
                }
                TestConsole.ReadLine();
            } 
            while (busCount > 0);
        }
        private void Check(Queue<int>[,] timeBoard, int index, bool[] checkBoard)
        {
            if (checkBoard[index])
            {
                return;
            }
            checkBoard[index] = true;
            for (int i = 0; i < checkBoard.Length; i++)
            {
                if (timeBoard[index, i] != null && timeBoard[index, i].Count > 0)
                {
                    Check(timeBoard, i, checkBoard);
                }
                if (timeBoard[i, index] != null && timeBoard[i, index].Count > 0)
                {
                    Check(timeBoard, i, checkBoard);
                }
            }
        }
        private int AddMeet(bool[,] meetBoard, int x, int y, int count)
        {
            int addCount = 0;
            if (!meetBoard[x, y])
            {
                meetBoard[x, y] = true;
                addCount++;
            }
            if (!meetBoard[y, x])
            {
                meetBoard[y, x] = true;
                addCount++;
            }
            for (int i = 0; i < count; i++)
            {
                if (meetBoard[x, i] && !meetBoard[y,i])
                {
                    meetBoard[y, i] = true;
                    addCount++;
                }
                if (meetBoard[y, i] && !meetBoard[x, i])
                {
                    meetBoard[x, i] = true;
                    addCount++;
                }
            }
            return addCount;
        }
        private Queue<int> GetMeetTime(List<int> a, List<int> b, int lcm)
        {
            Queue<int> meetTime = new Queue<int>();
            int indexA = 0;
            int indexB = 0;
            for (int i = 0; i <= lcm; i++)
            {
                if (a[indexA] == b[indexB])
                {
                    meetTime.Enqueue(i);
                }
                indexA++;
                if (indexA >= a.Count)
                {
                    indexA = 0;
                }
                indexB++;
                if (indexB >= b.Count)
                {
                    indexB = 0;
                }
            }
            return meetTime;
        }
    }
}
